package com.wc.alorithm_blue_bridge._日期问题.Q2096;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/10/16-13:58
 * 顺子日期
 * https://www.lanqiao.cn/problems/2096/learning/?page=3&first_category_id=1&sort=students_count
 */
public class Main {
    static int[] dayOfMonth = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static void main(String[] args) {
//        int[] ans = new int[]{20220123, 20221230, 20221231};
        Main main = new Main();
        int year = 2022;
        int day = 20220101;

        if (main.isRun(day / 10000)) {
            dayOfMonth[2] = 29;
        }

        int yearEnd = day / 10000 * 10000 + 12 * 100 + dayOfMonth[12];
        int ans = 0;
        while (day <= yearEnd) {
            if (main.isCircle(day)){
//                System.out.println(day);
                ans++;
            }
            day = main.nextDay(day);
        }
        System.out.println(ans);
    }

    /**
     * 是不是闰年
     * @param year
     * @return
     */
    public boolean isRun(int year) {
        if (year % 4 != 0) {
            return false;
        }
        return year % 100 != 0 || year % 400 == 0;
    }

    /**
     * 日期的下一天
     * @param day
     * @return
     */
    public int nextDay(int day) {
        int year = day / 10000;
        int month = day % 10000 / 100;
        int dayOfM = day % 100;
        if (isRun(year)) {
            dayOfMonth[2] = 29;
        }
        if (dayOfM < dayOfMonth[month]) {
            dayOfM += 1;
        } else {
            if (month + 1 == 13) {
                year += 1;
                month = 1;
            } else {
                month += 1;
            }
            dayOfM = 1;
        }
        return year * 10000 + month * 100 + dayOfM;
    }

    /**
     * 判断有没有顺子
     * @param day
     * @return
     */
    public boolean isCircle(int day) {
        String dayStr = String.valueOf(day);
        String[] compare = new String[]{"123","234","345","456","567","678","789","012"};

        for (String s : compare) {
            if (dayStr.contains(s)) {
                return true;
            }
        }

        return false;
    }
}
